package com.offcn.service.impl;


import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.offcn.dao.TCheckgroupCheckitemMapper;
import com.offcn.dao.TCheckitemMapper;
import com.offcn.pojo.TCheckgroupCheckitem;
import com.offcn.pojo.TCheckitem;
import com.offcn.service.TCheckitemService;
import com.offcn.util.PageResult;
import com.offcn.util.QueryPageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Component
@Service(interfaceClass = TCheckitemService.class)
public class TCheckitemServiceImpl implements TCheckitemService {
    @Autowired
    private TCheckitemMapper tCheckitemMapper;
    @Autowired
    private TCheckgroupCheckitemMapper checkgroupCheckitemMapper;
    @Override
    public PageResult findPage(QueryPageBean pageBean) {
        //要求顺序

        Page<TCheckitem> page = new Page<>(pageBean.getCurrentPage(),pageBean.getPageSize());
        QueryWrapper<TCheckitem>wrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(pageBean.getQueryString())){
            wrapper.eq("code",pageBean.getQueryString()).or().like("name",pageBean.getQueryString());
        }
        Page<TCheckitem> checkitemPage = tCheckitemMapper.selectPage(page, wrapper);
        PageResult pageResult = new PageResult(checkitemPage.getTotal(), checkitemPage.getRecords());
        return pageResult;
    }

    //添加功能  --  没有事务
    @Override
    public void add(TCheckitem checkitem) {
        tCheckitemMapper.insert(checkitem);
    }

    //回显
    @Override
    public TCheckitem findById(int id) {
        TCheckitem checkitem = tCheckitemMapper.selectById(id);
        return checkitem;
    }

    //修改
    @Override
    public void update(TCheckitem checkitem) {
        tCheckitemMapper.updateById(checkitem);
    }

    //删除  -- 多条sql
    @Override
    @Transactional
    public void delete(int id) {
        //多条SQL
        //先操作外键表
        QueryWrapper<TCheckgroupCheckitem> wrapper = new QueryWrapper<>();
        wrapper.eq("checkitem_id",id);
        checkgroupCheckitemMapper.delete(wrapper);
        //再操作主键表
        int i = tCheckitemMapper.deleteById(id);
        System.out.println(i);

    }

    @Override
    public List<TCheckitem> findAll() {

        return tCheckitemMapper.selectList(null);
    }
}
